package com.android.common.mvp.present;

import com.android.common.base.BaseObserver;
import com.android.common.base.BasePresentImpl;
import com.android.common.base.BaseView;
import com.android.common.constant.Constant;
import com.android.common.network.BusinessObserver;
import com.android.common.network.RetrofitManager;
import com.android.common.utils.FileUtil;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.logging.HttpLoggingInterceptor;

public class FilePresent extends BasePresentImpl<BaseView, FileApi> {

    public static final String DOWN_LOAD_FILE = "downloadFile";

    public FilePresent(BaseView view) {
        super(view);
    }

    public static final String UPLOAD_FILE = "uploadFile";

    public static final String UPLOAD_FILES = "uploadFiles";

    /**
     * 单个文件上传
     * 请参照https://www.w3school.com.cn/media/media_mimeref.asp
     *
     * @param file
     */
    public void uploadFile(File file) {
        RequestBody fileBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
        MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), fileBody);
        RetrofitManager<FileApi> retrofitManager = RetrofitManager.getInstance(HttpLoggingInterceptor.Level.BASIC);
        compose(retrofitManager.getApiService(FileApi.class).uploadFile(Constant.PROJECT_ID,Constant.ACCESS_LEVEL, part))
            .subscribe(new BusinessObserver<>(mBaseView,UPLOAD_FILE));
    }

    /**
     * 多个文件上传
     * 请参照https://www.w3school.com.cn/media/media_mimeref.asp
     *
     * @param files
     */
    public void uploadFiles(File[] files) {
        Map<String, RequestBody> map = new HashMap<>();
        for (File file : files) {
            RequestBody fileBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
            map.put("multipartFiles\"; filename=\"" + file.getName(), fileBody);
        }
        RetrofitManager<FileApi> retrofitManager = RetrofitManager.getInstance(HttpLoggingInterceptor.Level.BASIC);
        compose(retrofitManager.getApiService(FileApi.class).uploadFiles(Constant.PROJECT_ID, Constant.ACCESS_LEVEL, map))
            .subscribe(new BusinessObserver<>(mBaseView,UPLOAD_FILES));
    }

    /**
     * 多个文件上传
     * 请参照https://www.w3school.com.cn/media/media_mimeref.asp
     *
     * @param files
     */
    public void uploadFiles(List<File> files) {
        Map<String, RequestBody> map = new HashMap<>();
        for (File file : files) {
            RequestBody fileBody = RequestBody.create(file, MediaType.parse("multipart/form-data"));
            map.put("multipartFiles\"; filename=\"" + file.getName(), fileBody);
        }

        RetrofitManager<FileApi> retrofitManager = RetrofitManager.getInstance(HttpLoggingInterceptor.Level.BASIC);
        compose(retrofitManager.getApiService(FileApi.class).uploadFiles(Constant.PROJECT_ID, Constant.ACCESS_LEVEL, map))
            .subscribe(new BusinessObserver<>(mBaseView,UPLOAD_FILES) );
    }
    /**
     * 文件下载
     */
    public void downloadFile(String fileId, final String destDir, final String fileName) {
        RetrofitManager<FileApi> retrofitManager = RetrofitManager.getInstance(HttpLoggingInterceptor.Level.BASIC);
        retrofitManager.getApiService(FileApi.class)
            .downloadFileRecord(fileId)
            .subscribeOn(Schedulers.io())//请求网络 在调度者的io线程
            .observeOn(Schedulers.io()) //指定线程保存文件
            .observeOn(Schedulers.computation())
            .map(responseBody -> FileUtil.saveFileWithCallback(responseBody, destDir, fileName))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new BaseObserver<File>(mBaseView,DOWN_LOAD_FILE) {
                @Override
                public void onSuccess(File file) {
                    mBaseView.onSucceed(fileId,DOWN_LOAD_FILE);
                }

            });
    }

    /**
     * 文件下载
     *
     * @param url
     * @param destDir
     * @param fileName
     */
    public void downloadFileWithUrl(String url, final String destDir, final String fileName) {
        RetrofitManager<FileApi> retrofitManager = RetrofitManager.getInstance(HttpLoggingInterceptor.Level.BASIC);
        retrofitManager.getApiService(FileApi.class)
            .download(url)
            .subscribeOn(Schedulers.io())//请求网络 在调度者的io线程
            .observeOn(Schedulers.io()) //指定线程保存文件
            .observeOn(Schedulers.computation())
            .map(responseBody -> FileUtil.saveFileWithCallback(responseBody, destDir, fileName))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new BaseObserver<File>(mBaseView,DOWN_LOAD_FILE) {
                @Override
                public void onSuccess(File file) {
                    mBaseView.onSucceed(file,DOWN_LOAD_FILE);
                }

            });
    }
}
